package pl.edu.pw.polygen.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import pl.edu.pw.polygen.beans.UserBean;
import pl.edu.pw.polygen.core.user.UserService;

@Service("securityService")
public class UserDetailsServiceImpl implements UserDetailsService {

	@Autowired
	private UserService userService;
	
	public UserDetailsServiceImpl() {
		super();
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		try{
			UserBean userBean = userService.getUserByName(username);
			return new User(userBean.getId(),userBean.getLogin(),userBean.getPasswd(),new String[] { "ROLE_USER", "ROLE_ADMIN" });
		}catch(Exception e){
			throw new UsernameNotFoundException("Username not found.");
		}
	}

}
